﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyThuVien.Business;

namespace QuanLyThuVien.Presentation
{
    public partial class frmTKSach : Form
    {
        public frmTKSach()
        {
            InitializeComponent();
        }
        DauSachBUS BusSach = new DauSachBUS();

        TacGiaBUS BusTG = new TacGiaBUS();

        NXBBUS BusNXB = new NXBBUS();

        private void cbxChuyenNganh_CheckedChanged(object sender, EventArgs e)
        {
            if (cbxNhaXB.Checked == false && cbxTacGia.Checked == false && cbxTenSach.Checked == false)
            {
                KTCheckBox(cbxChuyenNganh, cbxTenSach, cbxTacGia, cbxNhaXB, "ChuyenNganh", lb1);
                BusSach.LoadCNLenCB(cbDuLieu1);
            }
            else
            {
                KTCheckBox(cbxChuyenNganh, cbxTenSach, cbxTacGia, cbxNhaXB, "ChuyenNganh", lb2);
                BusSach.LoadCNLenCB(cbDuLieu2);
            }
        }

        public void KTCheckBox(CheckBox ck1, CheckBox ck2, CheckBox ck3, CheckBox ck4, string dl1, Label lb1)
        {
            if (ck1.Checked == true && ck2.Checked == true)
            {
                ck3.Enabled = false;
                ck4.Enabled = false;
            }
            if (ck1.Checked == true && ck3.Checked == true)
            {
                ck2.Enabled = false;
                ck4.Enabled = false;
            }
            if (ck1.Checked == true && ck4.Checked == true)
            {
                ck3.Enabled = false;
                ck2.Enabled = false;
            }
            lb1.Text = dl1;
        }

        private void cbxTenSach_CheckedChanged(object sender, EventArgs e)
        {

            if (cbxChuyenNganh.Checked == true || cbxTacGia.Checked == true || cbxNhaXB.Checked == true)
            {
                KTCheckBox(cbxTenSach, cbxTacGia, cbxNhaXB, cbxChuyenNganh, "TenSach", lb2);
                BusSach.LoadDLLenCombobox(cbDuLieu2);
            }
            else
            {
                KTCheckBox(cbxTenSach, cbxTacGia, cbxNhaXB, cbxChuyenNganh, "TenSach", lb1);
               BusSach.LoadDLLenCombobox(cbDuLieu1);
            }
        }

        private void cbxTacGia_CheckedChanged(object sender, EventArgs e)
        {
            if (cbxChuyenNganh.Checked == true || cbxTenSach.Checked == true || cbxNhaXB.Checked == true)
            {
               KTCheckBox(cbxTacGia, cbxTenSach, cbxNhaXB, cbxChuyenNganh, "Tác giả", lb2);
               BusTG.LoadLenCBTacGia(cbDuLieu2);
                
            }
            else
            {
                KTCheckBox(cbxTacGia, cbxTenSach, cbxNhaXB, cbxChuyenNganh, "Tác giả", lb1);
                BusTG.LoadLenCBTacGia(cbDuLieu1);
            }
        }

        private void cbxNhaXB_CheckedChanged(object sender, EventArgs e)
        {
            if (cbxChuyenNganh.Checked == true || cbxNhaXB.Checked == true || cbxTacGia.Checked == true)
            {
                KTCheckBox(cbxNhaXB, cbxTacGia, cbxTenSach, cbxChuyenNganh, "Nhà XB :", lb2);
                BusNXB.LoadLenCB(cbDuLieu2);
            }
            else
            {
                KTCheckBox(cbxNhaXB, cbxTacGia, cbxTenSach, cbxChuyenNganh, "Nhà XB :", lb1);
                BusNXB.LoadLenCB(cbDuLieu2);
            }
        }

        private void btnChonLai_Click(object sender, EventArgs e)
        {
            cbxChuyenNganh.Enabled = true; cbxChuyenNganh.Checked = false;
            cbxTacGia.Enabled = true; cbxTacGia.Checked = false;
            cbxNhaXB.Enabled = true; cbxNhaXB.Checked = false;
            cbxTenSach.Enabled = true; cbxTenSach.Checked = false;
            cbDuLieu1.Items.Clear(); cbDuLieu1.Text = ""; lb1.Text = "";
            cbDuLieu2.Items.Clear(); cbDuLieu2.Text = ""; lb2.Text = "";
            BusSach.LayDLLenDataGridView(dgTraCuu);
        }

        private void btnMuon_Click(object sender, EventArgs e)
        {
            this.Close();
            frmQuanLyMuonSach frm = new frmQuanLyMuonSach();
            frm.ShowDialog();
        }

        private void btnTraCuu_Click(object sender, EventArgs e)
        {
            //if (cbxChuyenNganh.Checked==true && cbxTenSach.Checked==true)
            //{
                BusSach.TraCuuSachTu2TieuChi(dgTraCuu, lb1.Text, cbDuLieu1.Text, lb2.Text, cbDuLieu2.Text);
            //}
            //if (cbxNhaXB.Checked == true || cbxTacGia.Checked == true)
            //{
            //    BusSach.LoadLenDGTKSachTheoTG(dgTraCuu, cbDuLieu1.Text);
            //}
        }

        private void btnTraCuuNangCao_Click(object sender, EventArgs e)
        {
            BusSach.TimKiemSachNangCao(dgTraCuuNhanh, cbDLTimKiemNhanh.Text);
        }

        private void frmTKSach_Load(object sender, EventArgs e)
        {
            BusSach.LayDLLenDataGridView(dgTraCuu);
        }

        //private void cbDuLieu1_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    txtDL1.Text = BusNXB.LoadLenMaNXB(txtDL1.Text, cbDuLieu1.Text);
        //}

        //private void cbDuLieu2_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    txtDL2.Text = BusNXB.LoadLenMaNXB(txtDL2.Text, cbDuLieu1.Text);
        //}
    }
}
